<template>
  <div class="resource">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form :inline="true" ref="form" :model="form" class="demo-form-inline">
          <el-form-item prop="phone" label="手机号">
            <el-input v-model="form.phone"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :disabled="isLoading" @click="onSubmit">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button :disabled="isLoading" @click="onReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table
      :data="users"
      v-loading="isLoading"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="用户ID">
      </el-table-column>
      <el-table-column
        prop="portrait"
        label="头像">
        <template slot-scope="scope">
            <el-avatar shape="circle" :size="30" :src="scope.row.portrait || 'https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png'"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="用户名">
      </el-table-column>
      <el-table-column
        prop="phone"
        label="手机号">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="注册时间">
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
        <el-button @click="handleClick(scope.row.id)" size="small">编辑</el-button>
        <el-button type="danger" size="small" @click="handleClick(scope.row.id)">删除</el-button>
      </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :disabled="isLoading"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="form.current"
      :page-sizes="[10, 30, 50, 100]"
      :page-size="form.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
import Vue from 'vue'
import { getUserPages } from '@/services/user'
export default Vue.extend({
  name: 'UserList',
  data () {
    return {
      users: [],
      form: {
        phone: '',
        endCreateTime: '',
        startCreateTime: '',
        pageSize: 10, // 每页数量
        currentPage: 1 // 当前页
      },
      total: 0,
      isLoading: false,
      editId: null
    }
  },
  created () {
    this.loadUsers()
  },
  methods: {
    async loadUsers () {
      this.isLoading = true
      const { data } = await getUserPages(this.form)
      this.users = data.data.records
      this.total = data.data.total
      this.isLoading = false
    },
    onSubmit () {
      console.log(this.form)
      this.form.currentPage = 1
      this.loadUsers()
    },
    onReset () {
      this.form.currentPage = 1
      this.$refs.form.resetFields()
      this.loadUsers()
    },
    handleSizeChange (val) {
      this.form.pageSize = val
      this.form.currentPage = 1
      this.loadUsers()
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      this.form.currentPage = val
      this.loadCourses()
      console.log(`当前页: ${val}`)
    },
    handleClick (editId) {
      console.log(editId, 'getResource')
    }
  }
})
</script>

<style lang="scss" scoped>

</style>
